import webbrowser
import logging
port = '8000'

urls = ('/welcome', 'welcome',
        '/login', 'login',
        '^/user/([\w][\w]*)/groups/edit$', 'user_groups_edit',
        '^/user/([\w][\w]*)/edit$', 'user_edit',
        '^/user/([\w][\w]*)/groups$', 'user_groups',
        '/user/add', 'user_add',
        '^/user/([\w][\w]*)', 'user_read'
        )
testdata = [
('/user/new', 'Add a "buser" with email "buser@test.com"'),
('/user/new', 'Add another "buser"'),
('/user/new', 'Add any user with duplicate email "buser@test.com"'),
('/user/buser/edit','Change name to buser2'),
('/user/buser2/edit','Set to inactive'),
('/user/buser2/groups','Check group list is none'),
('/user/buser2/groups/edit','Add groups  group1, group2'),
('/user/buser2/groups/edit','Change to groups group2, group3'),
('/user/buser2/groups','Check group list is group2, group3'),
]

LOG_FILENAME = '/home/mervyn/Code/Owlspace/Trunk/owl/test/log/browse_log.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.INFO, format='%(asctime)s %(message)s')


if __name__ =='__main__':
    print 'Starting test run....'
    logging.info('STARTING TEST RUN')
    tried = 0
    succeeded = 0 
    for t in testdata:
        tried += 1 
        url = 'http://localhost:{0}{1}'.format(port, t[0])
        webbrowser.open(url, new=0, autoraise=True)
        prompt = "Go to {0} and: {1}".format(url, t[1])
        print prompt
        result = raw_input('pass fail exit? p/f/x\n')
        if result == 'x':
            logging.info('xxxxx Exiting browse test')
            break
        if result == 'f':
            problem = raw_input('What was the problem?: ')
            logging.info('>>>> FAIL')
            logging.info('    prompt: {0}'.format(prompt))
            logging.info('        {0}'.format(problem))
        else:
            succeeded += 1
    print 'Done!\n'
    stats = 'RESULT: {0} tests run, {1} succeeded, percent {2}%'.format(tried, succeeded, int(succeeded * 100/tried))
    print stats
    logging.info(stats)
    logging.info('='*20)
